//
//  MemberShipViewController.m
//  NewApp
//
//  Created by  on 12-6-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MemberShipViewController.h"
#import "LoginViewController.h"
#import "MemberRegisterViewController.h"
#import "RetrivePasswordViewController.h"
#import "ResetPasswordViewController.h"
#import "NAAccount.h"
#import "PathHelper.h"
#import "AppDelegate.h"
#import "MyOrdersViewController.h"
#import "MyQueryViewController.h"
#import "InquiryPriceViewController.h"

@interface MemberShipViewController ()
/**
 *  Build UI elements
 */
- (void) buildUserInterface;

@end

@implementation MemberShipViewController
@synthesize tableView,datasourceObjects;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.hidesBackButton = YES;
    [self buildUserInterface];
    
    [self.engine memHomeInfos];
    self.title = @"返回";
    self.tabBarItem.title = @"會員";
    self.tabBarItem.image = [UIImage imageNamed:@"member_icon.png"];
    [self.navigationController setViewControllers:[NSArray arrayWithObject:self]];
    
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSString *filePath = [[PathHelper documentDirectoryPathWithName:@"Acc"] 
                          stringByAppendingPathComponent:@"accounts.info"];
    
    NAAccount *acc = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSArray *array;
    if (nil == acc) {
        array = [[NSArray alloc] initWithObjects:@"會員登入",@"會員註冊",@"忘記密碼",@"我的購物車", @"我的詢價單", nil];
    }else {
        array = [[NSArray alloc] initWithObjects:@"登出會員",@"密碼修改",@"我的購物車",@"我的訂單",@"我的詢價單", nil];
    }
    
    self.datasourceObjects = array;
    [array release];
    
    [self.tableView reloadData];
    
}

- (void) dealloc {
    [tableView release];
    [datasourceObjects release];
    [super dealloc];
}


#pragma mark -
#pragma mark - Private methods

- (void) buildUserInterface {
 
    //Build table view and datasource
    UITableView *tbl = [[UITableView alloc] initWithFrame:CGRectMake(0.f, 25.f, 320.f, 367.f)                                      style:UITableViewStylePlain];
    tbl.delegate = self;
    tbl.dataSource = self;
    tbl.separatorStyle = UITableViewCellSeparatorStyleNone;
    tbl.backgroundColor = [UIColor colorWithRed:51.f/255.f green:51.f/255.f blue:51.f/255.f alpha:1.f];
    self.tableView = tbl;
    [tbl release];
    
    [self.view addSubview:self.tableView];
    
   
}

#pragma mark -
#pragma mark - Override methods

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        NSString *title = [dict objectForKey:@"top_title"];
        if (title != nil && title.length > 0)
            self.titleLabel.text = title;
        
        [dict release];
        
    }
}

#pragma mark -
#pragma mark - UITableView delegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *cellText = [self.datasourceObjects objectAtIndex:indexPath.row];
    if ([cellText isEqualToString:@"會員登入"]) {
        LoginViewController  *viewctrl = [[LoginViewController alloc] initWithNibName:@"LoginViewController" 
                                                                               bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:viewctrl animated:YES];
        [viewctrl release];
    }
    
    if ([cellText isEqualToString:@"會員註冊"]) {
        MemberRegisterViewController  *viewctrl = [[MemberRegisterViewController alloc] initWithNibName:@"MemberRegisterViewController" 
                                                                               bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:viewctrl animated:YES];
        [viewctrl release];
    }
    
    if ([cellText isEqualToString:@"忘記密碼"]) {
        RetrivePasswordViewController  *viewctrl = [[RetrivePasswordViewController alloc] initWithNibName:@"RetrivePasswordViewController" 
                                                                                                 bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:viewctrl animated:YES];
        [viewctrl release];
    }
    
    if ([cellText isEqualToString:@"我的購物車"]) {
        [AppDelegate theAppDelegate].tabBarController.selectedIndex = TABBAR_ITEM_SHOPPING_CAR;
    }
    
    if([cellText isEqualToString:@"我的訂單"]) {
    
        MyOrdersViewController *viewCtrl = [[MyOrdersViewController alloc] initWithNibName:nil bundle:nil];
        self.title = @"返回";
        [self.navigationController pushViewController:viewCtrl animated:YES];
        [viewCtrl release];
    }
    
    if ([cellText isEqualToString:@"我的詢價單"]) {
        //[AppDelegate theAppDelegate].tabBarController.selectedIndex = TABBAR_ITEM_SHOPPING_CAR;
        MyQueryViewController *viewCtrl = [[MyQueryViewController alloc] initWithNibName:nil bundle:nil];
        [self.navigationController pushViewController:viewCtrl animated:YES];
        [viewCtrl release];
        
    }
    
    if([cellText isEqualToString:@"我的詢價單查詢"]) {
        

    }
    
    if ([cellText isEqualToString:@"登出會員"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"確認要登出會員?" delegate:self 
                                              cancelButtonTitle:@"取消" 
                                              otherButtonTitles:@"確認", nil];
        [alert show];
        [alert release];
        
    }
    
    if ([cellText isEqualToString:@"密碼修改"]) {
        ResetPasswordViewController  *viewctrl = [[ResetPasswordViewController alloc] initWithNibName:@"ResetPasswordViewController" 
                                                                                                 bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:viewctrl animated:YES];
        [viewctrl release];
    }

}

#pragma mark -
#pragma mark - UITableView datasource

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasourceObjects.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *memberShipCellIdentifier = @"MembershipCellIdentifier";
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:memberShipCellIdentifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:memberShipCellIdentifier];
    }
    
    UIView *v = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    v.backgroundColor = (indexPath.row % 2 == 0) ? [UIColor colorWithRed:81.f/255.f green:81.f/255.f blue:81.f/255.f alpha:1.f] : [UIColor colorWithRed:51.f/255.f green:51.f/255.f blue:51.f/255.f alpha:1.f];
    cell.backgroundView = v;
    
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = [self.datasourceObjects objectAtIndex:indexPath.row];
    
    cell.accessoryType = (indexPath.row == 6) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;


    return cell;
}

#pragma mark -
#pragma mark - UIAlertView delegate

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.cancelButtonIndex != buttonIndex) {
        NSString *filePath = [[PathHelper documentDirectoryPathWithName:@"Acc"] 
                              stringByAppendingPathComponent:@"accounts.info"];
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        
        NSArray *array = [[NSArray alloc] initWithObjects:@"會員登入",@"會員註冊",@"忘記密碼",@"我的購物車",@"我的詢價單", nil];
        self.datasourceObjects = array;
        [array release];
        
        [self.tableView reloadData];
    }
}




@end
